﻿using LINQtoCSV;
using Lottery.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lottery.Real
{
    public class CsvSystemCombinationsSelector : ICombinationsSelector
    {
        public IEnumerable<CsvCombinationTemplate> CombinationsTemplate { get; set; }

        public int RequiredNumbersCount
        {
            get { return 26; }
        }

        public CsvSystemCombinationsSelector(string path) 
        {
            var inputFileDescription = new CsvFileDescription
            {
                SeparatorChar = ',',
                FirstLineHasColumnNames = true
            };

            var cc = new CsvContext();
            this.CombinationsTemplate = cc.Read<CsvCombinationTemplate>(path, inputFileDescription);
        }

        public IEnumerable<ICombination> SelectCombinations(IEnumerable<int> numbers)
        {
            var result = new List<Combination>();

            foreach (var combinationTemplate in this.CombinationsTemplate)
            {
                var combinationNumbers = new List<int>();

                combinationNumbers.Add(numbers.ElementAt(combinationTemplate.Num1 - 1));
                combinationNumbers.Add(numbers.ElementAt(combinationTemplate.Num2 - 1));
                combinationNumbers.Add(numbers.ElementAt(combinationTemplate.Num3 - 1));
                combinationNumbers.Add(numbers.ElementAt(combinationTemplate.Num4 - 1));
                combinationNumbers.Add(numbers.ElementAt(combinationTemplate.Num5 - 1));
                combinationNumbers.Add(numbers.ElementAt(combinationTemplate.Num6 - 1));

                result.Add(new Combination() { Numbers = combinationNumbers });
            }

            return result;
        }        
    }
}
